<?php

namespace app\index\controller;
use app\admin\model\Member;
use app\admin\model\MemberGroupModel;
use app\admin\model\MemberModel;
use think\Controller;
use think\Db;
use org\Verify;
use com\Geetestlib;
use app\admin\model\UserModel;
class Login extends Base
{

    /**
     * 登录页面
     * @return
     */
    public function index()
    {
        return $this->fetch();
    }

  /**
     * 注册页面
     * @return
     */
    public function register()
    {
        return $this->fetch();
    }



    /**
     * 登录操作
     * @return
     */
    public function doLogin()
    {
        $account = input("param.account");
        $password = input("param.password");

        if (config('verify_type') == 1) {
            $code = input("param.code");
        }
        
        $result = $this->validate(compact('account', 'password'), 'MemberValidate');
        if(true !== $result){
            return json(['code' => -5, 'url' => '', 'msg' => $result]);
        }
        $verify = new Verify();
        if (config('verify_type') == 1) {
            if (!$code) {
                return json(['code' => -4, 'url' => '', 'msg' => '请输入验证码']);
            }
            if (!$verify->check($code)) {
                return json(['code' => -4, 'url' => '', 'msg' => '验证码错误']);
            }
        }

        $hasUser = Db::name('member')->where('account', $account)->find();
        if(empty($hasUser)){
            return json(['code' => -1, 'url' => '', 'msg' => '用户不存在']);
        }

        if(md5(md5($password) . config('auth_key')) != $hasUser['password']){
            writelog($hasUser['id'],$account,'用户【'.$account.'】登录失败：密码错误',2);
            return json(['code' => -2, 'url' => '', 'msg' => '账号或密码错误']);
        }

        if(1 != $hasUser['status']){
            writelog($hasUser['id'],$account,'用户【'.$account.'】登录失败：该账号被禁用',2);
            return json(['code' => -6, 'url' => '', 'msg' => '该账号被禁用']);
        }


        //获取该管理员的角色信息
        $members = new MemberModel();
        
        session('uid', $hasUser['id']);         //用户ID
        session('account', $hasUser['account']);  //用户名
        session('head_img', $hasUser['head_img']); //用户头像

        $groups = new MemberGroupModel();
        $group = $groups->getOne($hasUser['group_id']);
        session('group_id', $group['id']);        //单位名称
        session('group_name', $group['group_name']);   //单位名称
  
        //更新管理员状态
        // $param = [
        //     'loginnum' => $hasUser['loginnum'] + 1,
        //     'last_login_ip' => request()->ip(),
        //     'last_login_time' => time(),
        //     'token' => md5($hasUser['username'] . $hasUser['password'])
        // ];

        // Db::name('admin')->where('id', $hasUser['id'])->update($param);
        // writelog($hasUser['id'],session('username'),'用户【'.session('username').'】登录成功',1);
        return json(['code' => 1, 'url' => url('index/index'), 'msg' => '登录成功！']);
    }


    /**
     * 注册操作
     * @return
     */
    public function doRegister()
    {
        $account = input("param.account");
        $password = input("param.password");
        //所属组织
        $group_id = input("param.group_id");
        $mobile = input("param.mobile");
        $nickname = input("param.nickname");
        
        $result = $this->validate(compact('account', 'password'), 'MemberValidate');
        if(true !== $result){
            return json(['code' => -5, 'url' => '', 'msg' => $result]);
        }

        $hasUser = Db::name('member')->where('account', $account)->find();
        if($hasUser){
            return json(['code' => -1, 'url' => '', 'msg' => '该用户已存在,请重新输入用户名']);
        }

    
        $map =[];
        $map['account']= $account;
        $map['password']= md5(md5($password) . config('auth_key'));
        $map['group_id']= $group_id;
        $map['status']= 1;
        $map['mobile']= $mobile;
        $map['nickname']= $nickname;
        $members = new MemberModel();
        $flag = $members->insertMember($map);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => '用户注册成功！']);
    }




    /**
     * 验证码
     * @return
     */
    public function checkVerify()
    {
        $verify = new Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
		$verify->codeSet = '0123456789';
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }


    /**
     * 退出登录
     * @return
     */
    public function loginOut()
    {
        session(null);
        cache('db_config_data',null);//清除缓存中网站配置信息
        $this->redirect('index/index');
    }
}